<template>
  <div>
    <VolBox :lazy="true" :model.sync="model" :title="'营销活动--' + title" :width="Mwidth"
      :height="Mheight">
      <VolForm ref="formRules1" :label-width="100" :loadKey="true" :formFields="formFields1"
        :formRules="formRules1"></VolForm>
      <VolForm v-if="
          formFields1.c_type === '0' ||
            formFields1.c_type === '1' ||
            formFields1.c_type === '8' ||
            formFields1.c_type === '10'
        " ref="myform2" :label-width="100" :loadKey="true" :formFields="formFields2"
        :formRules="formRules2">
      </VolForm>
      <VolForm v-if="formFields1.c_type === '1'" ref="myform3" :label-width="100" :loadKey="true"
        :formFields="formFields3" :formRules="formRules3">
      </VolForm>
      <VolForm v-else-if="formFields1.c_type === '10'" ref="myformzt" :label-width="120"
        :loadKey="true" :formFields="formFieldszt" :formRules="formRuleszt">
      </VolForm>
      <div v-else-if="formFields1.c_type === '2' || formFields1.c_type === '3'">
        <VolForm ref="myform4" :label-width="100" :loadKey="true" :formFields="formFields4"
          :formRules="formRules4"></VolForm>
        <el-form label-width="100px" :disabled="disabledformFields4" class="formFields5">
          <el-form-item label="优惠条件" class="ivu-form-item-label">
            <div class="yhtjint" v-for="(i, ii) in formlist" :key="ii">
              <span>消费满</span>
              <el-input size="small" v-model="i.fullPrice"></el-input>
              <span style="margin-left:10px">{{ listType }}</span>
              <el-input size="small" v-model="i.discount"></el-input>
              <el-button size="small" v-if="ii == 0" @click="btnClick(1, ii)"
                :disabled="buttonDisabled">+</el-button>
              <el-button size="small" v-else @click="btnClick(0, ii)"
                :disabled="buttonDisabled">-</el-button>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <div v-if="formFields1.c_type === '8'">
        <!-- 选择会员白名单 -->
        <el-transfer :titles="titles" :filterable="true" :right-default-checked="checkList"
          v-model="memberphones" :data="productsList"></el-transfer>
      </div>
      <el-button v-show="title != '查看' && ActivityTypeStatus" class="boxButton"
        :disabled="buttonDisabled" @click="addsp">商品选择</el-button>
      <vol-table v-show="ActivityTypeStatus" ref="octable" :loadKey="true" :columns="
          title == '新增' ? columns : buttonDisabled ? columnsc : columns
        " :tableData="goodsListInfo" :height="300" :summary="true" :columnIndex="true"
        :click-edit="true" :ck="false"></vol-table>
      <div class="from_table_box">
        <div class="header_box">
          <div class="tab">
            <!-- <div class="tab_item">
              充值规则
            </div> -->
          </div>
          <div class="button_box" v-show="RechargeTypeStatus">
            <div class="el-icon-plus button_item" @click="
                () => {
                  this.$refs.table_one.addRow();
                }
              ">
              &nbsp;添加行
            </div>
            <div class="el-icon-close button_item" @click="
                () => {
                  this.$refs.table_one.delRows();
                }
              ">
              &nbsp;删除行
            </div>
          </div>
        </div>
        <div class="table_box" v-show="RechargeTypeStatus">
          <vol-table ref="table_one" :loadKey="true" :columns="rechargeGift" :height="210"
            :defaultLoadPage="false" :row-index="true" :columnIndex="true" :index="true"
            :click-edit="true" :tableData="tableData" :paginationHide="true"></vol-table>
        </div>
      </div>
      <div slot="footer">
        <Button type="error" icon="md-checkmark" v-show="title != '查看'" @click="submit1">确认</Button>
        <Button type="info" icon="md-clos" @click="model = false">关闭</Button>
      </div>
    </VolBox>
    <vol-box :model.sync="box1" title="选择券或券包" :height="600" :width="1000" :padding="15">
      <div class="button_box">
        <div class="el-icon-plus button_item" @click="btnClick(table_one)">
          &nbsp;添加
        </div>
        <div class="el-icon-close button_item" @click="
                () => {
                  this.$refs.table_two.delRows();
                }
              ">
          &nbsp;删除行
        </div>
      </div>
      <vol-table ref="table_two" :loadKey="true" :columns="columnsTicket" :pagination-hide="false"
        :height="510" :columnIndex="false" :index="true" :click-edit="true"
        :tableData="ticketTableData" :paginationHide="true" :summary="true"></vol-table>
      <div slot="footer">
        <Button type="primary" @click="closeChild()">确认</Button>
      </div>
    </vol-box>
    <VolBox :lazy="true" :model.sync="model2" title="商品选择" :width="Mwidth" :height="Mheight">
      <div class="SPXZ">
        <el-tree ref="tree" :data="CodityClaifa" show-checkbox node-key="id" :props="defaultProps"
          @check="checkchange">
        </el-tree>
        <div class="volTable">
          <el-input class="int" v-model="name" placeholder="请输入商品名称"
            @keyup.enter.native="checkchange"></el-input>
          <vol-table ref="xztable" :loadKey="true" :columns="columns2" :tableData="goodsListInfo2"
            :height="430" :summary="true" :columnIndex="true" :click-edit="true"></vol-table>
        </div>
      </div>
      <div slot="footer">
        <Button type="error" icon="md-checkmark" @click="submit2">保存</Button>
        <Button type="info" icon="md-close" @click="model2 = false">关闭</Button>
      </div>
    </VolBox>
    <vol-box :model.sync="box2" title="编辑" :height="500" :width="600" :padding="15">
      <div class="cascader">
        <div class="casd">
          <span style="font-size:14px;margin-left: 5px;">类型:</span>
          <el-radio v-model="radio" label="1">券包</el-radio>
          <el-radio v-model="radio" label="2">券</el-radio>
        </div>
        <div class="casd" style="margin-top: 20px;" v-if="radio == '1'">
          <span style="font-size:14px;margin-left: 5px;">{{
            radio == 1 ? "券包:" : "券:"
          }}</span>
          <el-select v-model="voucherPackageData" clearable multiple placeholder="请选择"
            @change="changeselect($event)">
            <el-option v-for="item in voucherPackageList" :key="item.key" :label="item.value"
              :value="item.key">
            </el-option>
          </el-select>
        </div>
        <div class="casd" style="margin-top: 20px;" v-if="radio == '2'">
          <Button type="error" @click="addqList()">选择券</Button>
          <vol-table ref="XZQtable" :loadKey="true" :columns="columns4"
            :tableData="voucherPackageListGL" :height="430" :summary="true" :columnIndex="true"
            :click-edit="true"></vol-table>
        </div>
      </div>
      <div slot="footer">
        <Button type="default" @click="voucherChild()">确认</Button>
      </div>
    </vol-box>
    <vol-box :model.sync="box3" title="选择券" :height="500" :width="600" :padding="15">
      <vol-table ref="Qtable" :loadKey="true" :columns="columns3" :tableData="voucherPackageList"
        :height="430" :summary="true" :columnIndex="true" :click-edit="true"></vol-table>
      <div slot="footer">
        <Button type="default" @click="voucherChilds()">确认</Button>
      </div>
    </vol-box>
  </div>
</template>
<script>
import VolBox from "@/components/basic/VolBox.vue";
import VolForm from "@/components/basic/VolForm.vue";
import VolTable from "@/components/basic/VolTable.vue";
import Icons from "@/components/basic/Icons.vue";
var vueParam = {
  components: {
    VolBox,
    VolForm,
    VolTable,
    Icons,
  },
  data() {
    return {
      productsList: [],
      checkList: [],
      memberphones: [],
      titles: ["待选名单", "已选名单"],
      kaAccount: [],
      applicabilityDisabled: true,
      kaList: [],
      radio: "1",
      elementIdex: "",
      model: false,
      box1: false,
      box2: false,
      box3: false,
      Mwidth: 1000,
      Mheight: 500,
      voucherPackageData: [],
      voucherPackageDataName: [],
      voucherPackageList: [],
      voucherPackageListGL: [],
      // 活动类型状态
      tableData: [],
      tableDatabefore: [],
      // 充值赠送
      rechargeGift: [
        {
          field: "cardTemplate",
          title: "卡模版",
          type: "select",
          bind: { key: "", data: [] },
          width: 90,
          align: "left",
          edit: { type: "select" },
          onChange: (column, row, tableData, value) => {
            this.cardList(row.cardTemplate);
          },
        },
        {
          field: "cardList",
          title: "列表批号",
          type: "select",
          width: 160,
          align: "left",
          bind: { key: "", data: [] },
          edit: { type: "select" },
        },
        {
          field: "vouchersName",
          title: "活动名称",
          type: "decimal",
          width: 90,
          align: "left",
          edit: { type: "text" },
        },
        {
          field: "rechargeAmount",
          title: "充值金额",
          type: "string",
          width: 60,
          align: "left",
          hidden: false,
          edit: { type: "text" },
        },
        {
          field: "giftAmount",
          title: "赠送金额",
          type: "string",
          width: 60,
          align: "left",
          hidden: false,
          edit: { type: "text" },
        },
        {
          field: "discountAmount",
          title: "折扣金额",
          type: "string",
          width: 60,
          align: "left",
          hidden: false,
          edit: { type: "text" },
        },
        {
          field: "discountPrice",
          title: "折扣",
          type: "decimal",
          width: 90,
          align: "left",
          sortable: true,
          hidden: false,
          edit: { type: "text" },
        },

        {
          field: "children",
          title: "数量",
          type: "decimal",
          width: 90,
          align: "left",
          hidden: true,
        },
        {
          field: "vouchersRemark",
          title: "活动备注",
          type: "decimal",
          width: 90,
          align: "left",
          edit: { type: "text" },
        },
        {
          field: "Render",
          title: "操作",
          fixed: "right",
          width: 120,
          render: this.getRenderOperate(),
        },
      ],
      columnsTicket: [
        {
          field: "id",
          title: "产品ID",
          type: "string",
          width: 120,
          align: "left",
          hidden: true,
          require: true,
        },
        {
          field: "goodName",
          title: "商品名称",
          type: "string",
          width: 120,
          align: "left",
          hidden: false,
          require: true,
        },
        {
          field: "goodCode",
          title: "商品编码",
          type: "string",
          width: 120,
          align: "left",
          hidden: false,
          require: true,
        },
        {
          field: "hotel_ID",
          title: "酒店id",
          type: "string",
          width: 120,
          align: "left",
          hidden: true,
        },
        {
          field: "price",
          title: "平摊价",
          type: "decimal",
          width: 90,
          align: "left",
          sortable: true,
          hidden: false,
          edit: { type: "decimal", required: true },
        },
        {
          field: "number",
          title: "数量",
          type: "int",
          width: 120,
          align: "left",
          hidden: false,
          edit: { type: "number" },
        },
      ],
      ActivityTypeStatus: false,
      RechargeTypeStatus: false,
      title: "新建",
      formFields1: {
        c_name: "",
        start_time: "",
        end_time: "",
        timeContainer: [],
        c_type: "",
        distributionid: "",
        membertype: [],
        isShow: 1,
        isPublic: 1,
      },
      formRules1: [
        [
          {
            title: "分销商",
            field: "distributionid",
            type: "select",
            dataKey: "distributorList",
            data: [],
            required: true,
            disabled: false,
          },
          {
            title: "是否启用",
            field: "isShow",
            type: "switch",
            required: true,
            disabled: false,
          },
        ],
        [
          {
            title: "活动类型",
            field: "c_type",
            type: "select",
            // dataKey: "mall_activity_type",
            data: [],
            required: true,
            disabled: false,
          },
          {
            title: "活动名称",
            field: "c_name",
            type: "text",
            required: true,
            disabled: false,
          },
        ],
        [
          {
            title: "有效时间",
            field: "timeContainer",
            type: "datetime",
            required: true,
            range: true,
            disabled: false,
          },

          {
            title: "报名人数",
            field: "groupsize",
            type: "text",
            cont: "人",
            hidden: true,
          },
        ],
        [
          {
            title: "会员类型",
            field: "membertype",
            type: "selectList",
            dataKey: "Member_Type",
            data: [],
            hidden: true,
          },
          // {
          //   title: "表单名称",
          //   field: "fromId",
          //   type: "select",
          //   dataKey: "activity_ form",
          //   data: [],
          //   hidden: true
          // },
          {
            title: "是否公开",
            field: "isPublic",
            type: "switch",
            required: true,
            disabled: false,
          },
        ],
      ],
      formFields2: {
        sellingRulesid: "",
        refundrulesid: "",
        verificationrulesid: "",
        c_remark: "",
      },
      formRules2: [
        [
          {
            title: "售卖规则",
            required: true,
            field: "sellingRulesid",
            type: "select",
            dataKey: "",
            data: [],
            disabled: false,
          },
          {
            title: "退改规则",
            required: true,
            field: "refundrulesid",
            type: "select",
            dataKey: "",
            data: [],
            disabled: false,
          },
        ],
        [
          {
            title: "核销规则",
            required: true,
            field: "verificationrulesid",
            type: "select",
            dataKey: "",
            data: [],
            disabled: false,
          },
          {
            title: "活动说明",
            field: "c_remark",
            type: "text",
            disabled: false,
          },
        ],
      ],
      formFields3: {
        groupsize: "",
        purchaseseparately: "",
        virtualclustering: "",
        maximumvirtualnumber: "",
        attendancecarddisplay: "",
        groupeffectivetime: "",
      },
      formRules3: [
        [
          {
            title: "成团人数",
            field: "groupsize",
            type: "textAppend",
            cont: "人",
            disabled: false,
          },
          {
            title: "单独购买",
            field: "purchaseseparately",
            type: "switch",
            disabled: false,
          },
        ],
        [
          {
            title: "虚拟成团",
            field: "virtualclustering",
            type: "switch",
          },
          {
            title: "最多虚拟人数",
            field: "maximumvirtualnumber",
            type: "number",
            disabled: false,
          },
        ],
        [
          {
            title: "参团卡显示",
            field: "attendancecarddisplay",
            type: "switch",
            disabled: false,
          },
          {
            title: "组团有效时间",
            field: "groupeffectivetime",
            type: "textAppend",
            cont: "小时",
            disabled: false,
          },
        ],
      ],
      formFields4: {
        preferentialtype: "",
        c_remark: "",
      },
      formRules4: [
        [
          {
            title: "优惠类型",
            field: "preferentialtype",
            type: "radio",
            data: [
              {
                value: "消费金额",
                key: "0",
              },
              {
                value: "购买件数",
                key: "1",
              },
            ],
            disabled: false,
          },
          {
            title: "活动说明",
            field: "c_remark",
            type: "text",
            disabled: false,
          },
        ],
      ],
      formlist: [],
      disabledformFields4: false,
      ZB: {},
      listType: "",
      columns: [
        {
          field: "pic_path",
          title: "商品图片",
          type: "imgupgrade",
          width: 100,
          align: "center",
        },
        {
          field: "name",
          title: "商品名称",
          type: "string",
          width: 100,
          align: "center",
          fixed: "left",
        },
        {
          field: "start_time",
          title: "活动开始日期",
          type: "datetime",
          width: 100,
          align: "center",
          hidden: false,
          edit: { type: "datetime", keep: true },
        },
        {
          field: "end_time",
          title: "活动结束日期",
          type: "datetime",
          width: 100,
          align: "center",
          hidden: false,
          edit: { type: "datetime", keep: true },
        },
        {
          field: "price",
          title: "价格",
          align: "center",
          width: 50,
          hidden: false,
        },
        {
          field: "activityPrice",
          title: "活动价格",
          align: "center",
          width: 50,
          hidden: false,
          require: true,
          edit: { type: "text", keep: true },
        },
        {
          field: "activityStock",
          title: "活动商品数量",
          type: "string",
          width: 50,
          align: "center",
          require: true,
          edit: { type: "text", keep: true },
        },
        {
          field: "D",
          title: "操作",
          type: "string",
          render: (h, { row, column, index }) => {
            return h("div", { style: {} }, [
              h(
                "a",
                {
                  on: {
                    click: (e) => {
                      this.goodsListInfo.splice(index, 1);
                    },
                  },
                },
                "删除"
              ),
            ]);
          },
        },
      ],
      columnsc: [
        {
          field: "pic_path",
          title: "商品图片",
          type: "imgupgrade",
          width: 60,
          align: "center",
        },
        {
          field: "name",
          title: "商品名称",
          type: "string",
          width: 100,
          align: "center",
          fixed: "left",
        },
        {
          field: "start_time",
          title: "活动开始日期",
          type: "datetime",
          width: 100,
          align: "center",
          hidden: false,
          edit: { type: "datetime", keep: true },
        },
        {
          field: "end_time",
          title: "活动结束日期",
          type: "datetime",
          width: 100,
          align: "center",
          hidden: false,
          edit: { type: "datetime", keep: true },
        },
        {
          field: "price",
          title: "价格",
          align: "center",
          width: 50,
        },
        {
          field: "activityPrice",
          title: "活动价格",
          align: "center",
          width: 50,
        },
        {
          field: "activityStock",
          title: "活动商品数量",
          type: "string",
          width: 100,
          align: "center",
        },
        {
          field: "D",
          title: "操作",
          type: "string",
          hidden: false,
          render: (h, { row, column, index }) => {
            return h("div", { style: {} }, [
              h(
                "a",
                {
                  style: { "margin-right": "10px" },
                  on: {
                    click: (e) => {
                      console.log(row);
                      this.$router.push({
                        name: "V_productbydate",
                        params: {
                          id: row.distriproductid,
                          type: 2
                        },
                      });
                    },
                  },
                },
                "价格日历"
              ),
            ]);
          },
        },
      ],
      goodsListInfo: [],
      buttonDisabled: false,
      goodsListInfo2: [],
      model2: false,
      columns2: [
        {
          field: "pic_path",
          title: "商品图片",
          type: "imgupgrade",
          width: 100,
          align: "center",
        },
        {
          field: "name",
          title: "商品名称",
          type: "string",
          width: 100,
          align: "center",
        },
        {
          field: "price",
          title: "价格",
          align: "center",
          width: 50,
        },
      ],
      columns3: [
        {
          field: "id",
          title: "ID",
          type: "string",
          width: 60,
          align: "center",
        },
        {
          field: "coupon_make_name",
          title: "券名称",
          type: "string",
          width: 100,
          align: "center",
        },
      ],
      columns4: [
        {
          field: "coupon_make_name",
          title: "券名称",
          type: "string",
          width: 100,
          align: "center",
        },
        {
          field: "CouponNum",
          title: "数量",
          type: "string",
          width: 100,
          align: "center",
          edit: { type: "text", keep: true },
        },
      ],
      CodityClaifa: [],
      ticketTableData: [],
      name: "",
      defaultProps: {
        children: "children",
        label: "label",
      },
      formFieldszt: {
        isSelect: "",
        groupsize: "",
        groupsizeMax: "",
        groupeffectivetime: "",
        is_operate: ''
      },
      formRuleszt: [
        [
          {
            title: "团长退款类型",
            field: "is_operate",
            type: "select",
            dataKey: '',
            data: [
              {
                key: 0,
                value: '团长退款拼团失败'
              },
              {
                key: 1,
                value: '团长退款不影响其他团员'
              }
            ]
          },
        ],
        [
          {
            title: "是否可选当天",
            field: "isSelect",
            type: "switch",
          },

          {
            title: "组团有效时间",
            field: "groupeffectivetime",
            type: "textAppend",
            cont: "小时",
            required: true,
          },
        ],
        [
          {
            title: "最多成团人数",
            field: "groupsizeMax",
            type: "text",
            required: true,
          },
          {
            title: "最少成团人数",
            field: "groupsize",
            type: "text",
            required: true,
          },
        ],
      ],
    };
  },
  watch: {
    model(v, n) {
      if (v) {
        this.updateDis();
      }
    },
    "formFields1.c_type": {
      handler(v, n) {
        switch (v) {
          case "0":
            this.ActivityTypeStatus = true;
            this.RechargeTypeStatus = false;
            break;
          case "1":
            this.ActivityTypeStatus = true;
            this.RechargeTypeStatus = false;
            break;
          case "2":
            this.ActivityTypeStatus = true;
            this.RechargeTypeStatus = false;
            if (this.title == "新建") {
              this.formlist = [
                {
                  fullPrice: "",
                  discount: "",
                },
              ];
            }

            this.listType = "优惠金额";
            break;
          case "3":
            this.ActivityTypeStatus = true;
            this.RechargeTypeStatus = false;
            if (this.title == "新建") {
              this.formlist = [
                {
                  fullPrice: "",
                  discount: "",
                },
              ];
            }
            this.listType = "折扣";
            break;
          case "5":
            this.rechargeGift.forEach((item) => {
              if (item.field == "rechargeAmount") {
                item.hidden = false;
              } else if (item.field == "giftAmount") {
                item.hidden = false;
              } else if (item.field == "vouchersName") {
                item.hidden = false;
              } else if (item.field == "vouchersRemark") {
                item.hidden = false;
              } else if (item.field == "cardTemplate") {
                item.bind.data = this.kaAccount;
                item.hidden = false;
              } else if (item.field == "cardList") {
                // item.bind.data=this.kaList
                item.hidden = false;
              } else {
                item.hidden = true;
              }
            });
            this.RechargeTypeStatus = true;
            this.ActivityTypeStatus = false;
            console.log("充值赠送");

            break;
          case "6":
            this.rechargeGift.forEach((item) => {
              if (item.field == "discountAmount") {
                item.hidden = false;
              } else if (item.field == "discountPrice") {
                item.hidden = false;
              } else if (item.field == "vouchersName") {
                item.hidden = false;
              } else if (item.field == "vouchersRemark") {
                item.hidden = false;
              } else if (item.field == "cardTemplate") {
                item.bind.data = this.kaAccount;
                item.hidden = false;
              } else if (item.field == "cardList") {
                item.hidden = false;
              } else {
                item.hidden = true;
              }
            });
            console.log("充值折扣");
            this.ActivityTypeStatus = false;
            this.RechargeTypeStatus = true;
            break;
          case "7":
            this.rechargeGift.forEach((item) => {
              if (item.field == "rechargeAmount") {
                item.hidden = false;
              } else if (item.field == "vouchersNum") {
                item.hidden = false;
              } else if (item.field == "Render") {
                item.hidden = false;
              } else if (item.field == "vouchersName") {
                item.hidden = false;
              } else if (item.field == "vouchersRemark") {
                item.hidden = false;
              } else if (item.field == "cardTemplate") {
                item.bind.data = this.kaAccount;
                item.hidden = false;
              } else if (item.field == "cardList") {
                item.bind.data = this.kaList;
                item.hidden = false;
              } else {
                item.hidden = true;
              }
            });
            this.RechargeTypeStatus = true;
            this.ActivityTypeStatus = false;
            console.log("充值送券");
            break;
          case "8":
            let fromId = this.getOption("fromId");
            let membertype = this.getOption("membertype");
            fromId.hidden = false;
            membertype.hidden = false;
            this.RechargeTypeStatus = false;
            this.ActivityTypeStatus = true;
            console.log("报名活动");
            break;
          case "10":
            this.ActivityTypeStatus = true;
            this.RechargeTypeStatus = false;
            this.formFieldszt.is_operate = 0
            break;
        }
        let start_time = this.getColumns("start_time");
        let end_time = this.getColumns("end_time");
        let activityPrice = this.getColumns("activityPrice");
        let activityStock = this.getColumns("activityStock");
        let start_time2 = this.getColumnssc("start_time");
        let end_time2 = this.getColumnssc("end_time");
        let D = this.getColumnssc("D");
        let activityStock2 = this.getColumnssc("activityStock");
        let activityPrice2 = this.getColumnssc("activityPrice");
        start_time.hidden = v == 10 ? false : true;
        end_time.hidden = v == 10 ? false : true;
        start_time2.hidden = v == 10 ? false : true;
        end_time2.hidden = v == 10 ? false : true;
        D.hidden = v == 10 ? false : true;
      },
      deep: true,
      immediate: true,
    },
    radio: {
      handler(v, n) {
        if (v == 1) {
          this.voucherPackageData = [];
          this.getVoucherPackage();
        } else {
          this.voucherPackageData = [];
          this.getVoucher();
        }
      },
      deep: true,
      immediate: true,
    },
  },
  created() {
    this.GetProductType();
    this.cardTemplate();
  },
  mounted() {
    // 会员类型
    this.$nextTick(() => {
      let membertype = this.getOption("membertype");
      membertype.onChange = (val, option) => {
        let options = {
          order: "desc",
          page: 1,
          rows: 100,
          sort: "membertypeid",
          value: val,
          wheres: "[]",
        };
        this.http.post("/api/MebAccount/getPageData", options).then((res) => {
          this.productsList = [];
          if (res.rows.length && res.rows.length != 0) {
            this.productsList = res.rows.map((i) => {
              return {
                label: i.member_name + i.member_phone || "",
                value: i.memberid,
                key: i.member_phone,
              };
            });
          }
        });
      };
    });
  },
  methods: {
    getColumns(field) {
      let option;
      this.columns.forEach((x) => {
        if (x.field == field) {
          option = x;
        }
      });
      return option;
    },
    getColumnssc(field) {
      let option;
      this.columnsc.forEach((x) => {
        if (x.field == field) {
          option = x;
        }
      });
      return option;
    },
    renderFunc(h, option) {
      return option.name.length > 8 ? (
        <el-tooltip effect="light" content={option.name} placement="top">
          <span>{option.name}</span>
        </el-tooltip>
      ) : (
        <span>{option.name}</span>
      );
    },
    getRenderOperate() {
      return (h, { row, column, index }) => {
        return h("div", { style: {} }, [
          h(
            "a",
            {
              props: {},
              style: { "border-bottom": "1px solid" },
              on: {
                click: (e) => {
                  console.log(row, "券");
                  this.voucherPackageData = [];
                  this.elementIdex = row.elementIdex;
                  if (row.children != undefined) {
                    this.radio = row.children.radio;
                    if (this.radio == "1") {
                      this.voucherPackageData = row.children.voucherPackageData;
                    } else {
                      this.voucherPackageListGL =
                        row.children.voucherPackageListGL;
                    }
                  }
                  this.box2 = true;
                },
              },
            },
            "编辑  "
          ),
        ]);
      };
    },
    loadBefore(params, callback) {
      //设置查询 条件
      var _row;
      //获取主表选中的行
      this.$emit("parentCall", ($vue) => {
        var rows = $vue.getSelectRows();
        if (rows.length) {
          _row = rows[0];
        }
      });
      if (!_row) {
        //取消选中，清空明细
        return (this.$refs.tableList.rowData.length = 0);
        //  return this.$Message.error("请选中主表行数据");
      }
      //设置查询条件，用主表id加载明细表数据(如果是自己定义的接口，这里条件自己处理)
      params.value = _row.Order_Id;
      //设置排序字段(如果是自己定义的接口，排序字段不一定填写)
      if (!params.sort) {
        params.sort = "CreateDate";
      }
      return callback(true);
    },
    getOption(field) {
      let option;
      this.formRules1.forEach((x) => {
        x.forEach((item) => {
          if (item.field == field) {
            option = item;
          }
        });
      });
      return option;
    },
    getOption2(field) {
      let option;
      this.formRules2.forEach((x) => {
        x.forEach((item) => {
          if (item.field == field) {
            option = item;
          }
        });
      });
      return option;
    },
    checkchange() {
      let v = this.$refs.tree.getCheckedNodes();
      this.http
        .post("api/Mall_good/GetMallActivityGoods", {
          name: this.name,
          typeid: v.length > 0 ? v.map((i) => i.value).join(",") : "",
          start_time: this.formFields1.timeContainer[0],
          end_time: this.formFields1.timeContainer[1],
        })
        .then((res) => {
          res.data.map((_) => {
            if (_.pic_path) {
              _.pic_path = _.pic_path.split(",");
            } else {
              _.pic_path = [];
            }
          });
          this.goodsListInfo2 = res.data;
          this.$nextTick(() => {
            this.goodsListInfo.forEach((item) => {
              this.$refs.xztable.$refs.table.toggleRowSelection(
                this.goodsListInfo2.find((tx) => {
                  return tx.goods_id == item.goods_id;
                }),
                true
              );
            });
          });
        });
    },
    updateDis() {
      let formRulesNew = [
        ...this.formRules1,
        ...this.formRules2,
        ...this.formRules3,
        ...this.formRules4,
        ...this.formRuleszt,
      ];
      if (this.title == "查看") {
        formRulesNew.forEach((i) => {
          i.forEach((j) => {
            j.disabled = true;
          });
        });
        this.disabledformFields4 = true;
        this.buttonDisabled = true;
        this.columnsc.forEach((i) => {
          if (i.field == "activityStock") {
            i.edit = null;
          } else if (i.field == "activityPrice") {
            i.edit = null;
          }
        });
      } else {
        if (this.title == "编辑") {
          this.buttonDisabled = true;
          this.columnsc.forEach((i) => {
            if (i.field == "activityStock") {
              i.edit = { type: "text", keep: true };
            } else if (i.field == "activityPrice") {
              i.edit = { type: "text", keep: true };
            }
          });
          if (
            new Date().getTime() -
              new Date(this.formFields1.start_time).getTime() >
            0
          ) {
            // 进行中
            formRulesNew.forEach((i) => {
              i.forEach((j) => {
                switch (j.field) {
                  case "distributionid":
                    j.disabled = true;
                    break;
                  case "c_type":
                    j.disabled = true;
                    break;
                  case "c_name":
                    j.disabled = true;
                    break;
                  default:
                    j.disabled = false;
                    break;
                }
              });
            });
            this.disabledformFields4 = true;
          } else {
            // 未开始
            this.disabledformFields4 = false;
            formRulesNew.forEach((i) => {
              i.forEach((j) => {
                j.disabled = false;
              });
            });
          }
        } else {
          this.buttonDisabled = false;
          this.disabledformFields4 = false;
          this.formlist = [
            {
              fullPrice: "",
              discount: "",
            },
          ];
          formRulesNew.forEach((i) => {
            i.forEach((j) => {
              j.disabled = false;
            });
          });
          this.goodsListInfo = [];
          this.goodsListInfo2 = [];
        }
      }
    },
    btnClick(v, b) {
      if (v == 0) {
        this.formlist.splice(b, 1);
      } else {
        this.formlist.push({
          fullPrice: "",
          discount: "",
        });
      }
    },
    rowClick({ row, column, event }) {
      this.$refs.octable.$refs.table.toggleRowSelection(row);
    },
    addsp() {
      this.model2 = true;
      this.name = "";
      if (this.title == "新建") {
        if (this.goodsListInfo.length == 0) {
          this.$nextTick(() => {
            this.$refs.tree.setCheckedNodes([]);
          });
        }
      }
    },
    addqList() {
      this.box3 = true;
    },
    submit2() {
      this.goodsListInfo = this.$refs.xztable.getSelected();
      if (this.goodsListInfo.length == 1) {
        this.model2 = false;
      } else {
        this.$Message.error("只能选择一条商品数据");
      }
    },
    setprefreialtems(e) {
      if (e) {
        this.formlist = e;
      } else {
        this.formlist = [
          {
            fullPrice: "",
            discount: "",
          },
        ];
      }
    },

    GetProductType() {
      let sellingRulesid2 = this.getOption2("sellingRulesid");
      let refundrulesid2 = this.getOption2("refundrulesid");
      let verificationrulesid2 = this.getOption2("verificationrulesid");
      let c_type = this.getOption("c_type");
      this.http
        .post("/api/Dictionary/GetVueDictionary", [
          "mall_sellingrules",
          "Mall_refundrules",
          "mall_verificationrules",
          "Member_Type",
          "mall_activity_type",
        ])
        .then((res) => {
          if (res) {
            res.forEach((i) => {
              switch (i.dicNo) {
                case "mall_sellingrules":
                  sellingRulesid2.data = i.data;
                  break;
                case "Mall_refundrules":
                  refundrulesid2.data = i.data;
                  break;
                case "mall_verificationrules":
                  verificationrulesid2.data = i.data;
                  break;
                case "mall_activity_type":
                  let arr = [];
                  i.data.forEach((item) => {
                    if (item.value != "报名活动") {
                      arr.push(item);
                    }
                    c_type.data = arr;
                  });
                  break;
              }
            });
          }
        });
      this.http.get("api/producttypes/GetProductType").then((res) => {
        function arrReduce(someArr) {
          for (let i = 0; i < someArr.length; i++) {
            if (someArr[i].children) {
              arrReduce(someArr[i].children);
              someArr[i].label = someArr[i].typeName;
              someArr[i].value = someArr[i].id;
            } else {
              someArr[i].label = someArr[i].typeName;
              someArr[i].value = someArr[i].id;
            }
          }
        }
        arrReduce(res.data);
        this.CodityClaifa = res.data;
      });
    },
    submit1() {
      this.$refs.formRules1.validate(() => {
        this.$refs.myform2.validate(() => {
          let a = JSON.parse(JSON.stringify(this.$refs.octable.tableData));
          a.map((_) => {
            if (_.pic_path.length) {
              _.pic_path = _.pic_path.join();
            } else {
              _.pic_path = "";
            }
          });
          let sku = a;
          let status = true;
          if (this.formFields1.c_type == "10") {
            if (sku.length != 0) {
              status = sku.some((i) => {
                if (
                  i.start_time == undefined ||
                  i.start_time == undefined ||
                  i.start_end == "" ||
                  i.start_end == ""
                ) {
                  return false;
                } else {
                  // i.activityPrice = 0;
                  // i.activityStock = 0;
                  return true;
                }
              });
            } else {
              status = false;
            }
          } else {
            status = sku.some((i) => {
              if (
                i.activityPrice == undefined ||
                i.activityStock == undefined ||
                i.activityPrice == "" ||
                i.activityStock == ""
              ) {
                return false;
              } else {
                return true;
              }
            });
          }

          console.log(status);
          if (status) {
            let params = {
              mainData: {},
              detailData: [...sku],
            };
            this.formFields1.start_time = this.formFields1.timeContainer[0];
            this.formFields1.end_time = this.formFields1.timeContainer[1];
            switch (this.formFields1.c_type) {
              case "0":
                params.mainData = {
                  ...this.formFields1,
                  ...this.formFields2,
                };
                break;
              case "1":
                params.mainData = {
                  ...this.formFields1,
                  ...this.formFields2,
                  ...this.formFields3,
                };
                break;
              case "2":
                params.mainData = {
                  ...this.formFields1,
                  ...this.formFields4,
                  preferentialterms: JSON.stringify(this.formlist),
                };
                break;
              case "3":
                params.mainData = {
                  ...this.formFields1,
                  ...this.formFields4,
                  preferentialterms: JSON.stringify(this.formlist),
                };
                break;
              case "5":
                params.mainData = {
                  ...this.formFields1,
                  // ...this.formFields2,
                  rechargeRules: JSON.stringify(this.tableData),
                };

                break;
              case "6":
                params.mainData = {
                  ...this.formFields1,
                  // ...this.formFields2,
                  rechargeRules: JSON.stringify(this.tableData),
                };
                break;
              case "7":
                params.mainData = {
                  ...this.formFields1,
                  // ...this.formFields2,
                  rechargeRules: JSON.stringify(this.tableData),
                };
                break;
              case "8":
                params.mainData = {
                  ...this.formFields1,
                  ...this.formFields2,
                  rechargeRules: JSON.stringify(this.tableData),
                  preferentialterms: JSON.stringify(this.formlist),
                  memberphones:
                    this.memberphones.length != 0
                      ? this.memberphones.join(",")
                      : "",
                  membertypes:
                    this.formFields1.membertype.length != 0
                      ? this.formFields1.membertype.join(",")
                      : "",
                };
                break;
              case "10":
                params.mainData = {
                  ...this.formFields1,
                  ...this.formFields2,
                  ...this.formFieldszt,
                  rechargeRules: JSON.stringify(this.tableData),
                  membertypes:
                    this.formFields1.membertype.length != 0
                      ? this.formFields1.membertype.join(",")
                      : "",
                };
                break;
            }
            if (this.title == "新建") {
              params.detailData.forEach((i) => {
                i.activityNowStock = i.activityStock;
              });
              this.http
                .post("/api/mall_activity/AddActivity", params)
                .then((res) => {
                  if (res.status) {
                    this.model = false;
                    this.$Message.success("保存成功!");
                    this.$store.getters.data().viewPageTest.refresh();
                  } else {
                    this.$Message.error(res.message);
                  }
                });
            } else {
              params.mainData.c_id = this.ZB.c_id;
              if (params.detailData.length > 0) {
                params.detailData.forEach((i) => {
                  i.c_activityid = this.ZB.c_id;
                  i.merchantCode = this.ZB.merchantCode;
                  i.merchantName = this.ZB.merchantName;
                });
              }
              this.http
                .post("/api/mall_activity/UpdateActivity", params)
                .then((res) => {
                  if (res.status) {
                    this.model = false;
                    this.$Message.success("保存成功!");
                    this.$store.getters.data().viewPageTest.refresh();
                  } else {
                    this.$Message.error(res.message);
                  }
                });
            }
          } else {
            if (this.formFields1.c_type == "10") {
              if (sku.length != 0) {
                this.$Message.error("请输入活动开始日期和活动结束日期");
              } else {
                this.$Message.error("请选择商品");
              }
            } else {
              this.$Message.error("请输入活动价格和活动商品数量");
            }
          }
        });
      });
    },
    // 获取券或者券包下拉
    getVoucherPackage() {
      let params = {
        order: "desc",
        page: 1,
        rows: 100000000000000,
        sort: "id",
        wheres: '[{"name":"coupon_type","value":"1","displayType":"text"}]',
      };
      this.http.post("/api/MebcouponMake/getPageData", params).then((res) => {
        this.voucherPackageList = [];
        res.rows.forEach((i) => {
          let obj = {
            key: i.coupon_makeid,
            value: i.coupon_name,
          };
          this.voucherPackageList.push(obj);
        });
      });
    },
    getVoucher() {
      let params = {
        order: "desc",
        page: 1,
        rows: 100000000000000,
        sort: "id",
        wheres: "[]",
      };
      this.http.post("/api/MebCouponhis/getPageData", params).then((res) => {
        this.voucherPackageList = [];
        this.voucherPackageList = res.rows;
        // console.log(this.voucherPackageList, "this.voucherPackageList");
        // res.rows.forEach(i => {
        //   let obj = {
        //     key: i.id,
        //     value: i.coupon_make_name
        //   };
        //   this.voucherPackageList.push(obj);
        // });
      });
    },
    voucherChild() {
      this.box2 = false;
      if (this.radio == "1") {
        this.isVoucher();
      } else {
        this.isVouchers();
      }
    },
    isVoucher() {
      this.tableData.forEach((item) => {
        if (item.elementIdex == this.elementIdex) {
          item.children = {
            radio: this.radio,
            voucherPackageData: this.voucherPackageData,
            voucherPackageDataName: this.voucherPackageDataName,
          };
        }
      });
    },
    isVouchers() {
      this.tableData.forEach((item) => {
        if (item.elementIdex == this.elementIdex) {
          item.children = {
            radio: this.radio,
            voucherPackageListGL: this.voucherPackageListGL,
          };
        }
      });
    },
    voucherChilds() {
      this.box3 = false;
      this.voucherPackageListGL = this.$refs.Qtable.getSelected();
    },
    changeselect(val) {
      // console.log(val, "this.voucherPackageDataName");
      if (this.radio == "1") {
        this.voucherPackageDataName = [];
        this.voucherPackageList.forEach((item) => {
          for (let i = 0; i < val.length; i++) {
            if (item.key == val[i]) {
              let obj = {
                key: item.key,
                value: item.value,
              };
              this.voucherPackageDataName.push(obj);
            }
          }
        });
      } else {
        this.voucherPackageDataName = [];
        this.voucherPackageList.forEach((item) => {
          for (let i = 0; i < val.length; i++) {
            if (item.key == val[i]) {
              let obj = {
                key: item.key,
                value: item.value,
              };
              this.voucherPackageDataName.push(obj);
            }
          }
        });
      }
    },
    // 卡模版
    cardTemplate() {
      let params = {
        page: 1,
        rows: 5000000000,
        sort: "id",
        order: "desc",
        wheres: '[{"name":"basetype","value":"1","displayType":"select"}]',
      };
      // this.http.post("/api/MebCoupon/CouponGetPageData", params).then(res => {
      //   this.zqAccount = res.rows.map(i => {
      //     return {
      //       key: i.code,
      //       value: i.name
      //     };
      //   });
      // });

      // 卡模板
      this.http.post("/api/MebCardtype/CardGetPageData", params).then((res) => {
        this.kaAccount = res.rows.map((i) => {
          return {
            key: i.cardtypeid.toString(),
            value: i.cardtypename,
          };
        });
      });
      console.log(this.kaAccount, "kaAccount");
    },
    // 卡列表
    cardList(key) {
      let params = {
        page: 1,
        rows: 500,
        sort: "id",
        order: "desc",
        wheres: `[{\"name\":\"card_type\",\"value\":\"${key}\",\"displayType\":\"text\"}]`,
      };
      this.http
        .post("/api/member_card_make/CardMakeGetPageData", params)
        .then((res) => {
          if (res.rows != null && res.rows.length != 0) {
            this.kaList = res.rows.map((i) => {
              return {
                key: i.card_batch,
                // value: i.note
                value: i.card_batch,
              };
            });
            this.rechargeGift.forEach((item) => {
              if (item.field == "cardList") {
                item.bind.data = this.kaList;
              }
            });
            console.log(this.kaList, "this.kaList");
          } else {
            // couponcardCode.data = [];
            // this.editFormFields.couponcardCode = "";
          }
        });
    },
  },
};
export default vueParam;
</script>
<style lang="less">
.formFields5 {
  .el-form-item__label {
    color: #939394;
    font-size: 12px;
  }
  .yhtjint {
    font-size: 12px;
  }
  .el-input {
    width: 70px;
  }
}
</style>
<style lang="less" scoped>
.SPXZ {
  display: flex;
  justify-content: space-between;
  .volTable {
    width: 60%;
  }
  .el-tree {
    width: 40%;
    height: 450px;
    overflow-y: auto;
  }
}
.tjBut {
  margin: 10px;
}
.boxButton {
  margin-bottom: 10px;
}
.boxButtons {
  width: 100%;
  margin-bottom: 10px;
  margin-top: 10px;
}
.searchDiv {
  display: flex;
  justify-content: space-around;
  .int {
    margin-left: 10px;
  }
  .el-button {
    height: 40px;
    margin-left: 10px;
  }
}
</style>
